package com.nk.generate;

import com.nk.generate.constant.EntityConstant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


public class GeneratorUtil {

    private static final Log logger = LogFactory.getLog(GeneratorUtil.class);

    /**
     * 方法功能说明:代码自动生成器
     *
     * @param type:oracle/mysql
     * @param url:jdbc.url
     * @param schema：数据库登录帐号
     * @param password：数据库登录密码
     * @param tablefix：表名前缀
     * @param columnsfix：字段前缀
     * @param tables：要生成得表名
     * @param extendPackage：继承父类包名
     * @param extendName：继承父类类名
     * @param packageName：输出的包名
     */
    public static void generator(String type, String url, String schema, String password, String tablefix, String columnsfix, List<String> tables, String extendPackage, String extendName, String packageName) {
        Properties pro = new Properties();
        if (type.equals("mysql")) {
            pro.put("jdbc.driver", "com.mysql.jdbc.Driver");
        } else if (type.equals("oracle")) {
            pro.put("jdbc.driver", "oracle.jdbc.driver.OracleDriver");
        } else {
            throw new IllegalArgumentException("????????????!");
        }

        pro.put("jdbc.url", url);
        pro.put("user", schema);
        pro.put("password", password);
        pro.put("remarksReporting", "true");
        pro.put(EntityConstant.TABPREFIX, tablefix);
        pro.put(EntityConstant.ATTPREFIX, columnsfix);
        pro.put(EntityConstant.ISNEEDKEY, false);
        pro.put(EntityConstant.EXTEND_PACKAGE, extendPackage);
        pro.put(EntityConstant.EXTEND_NAME, extendName);
        pro.put(EntityConstant.PACKAGE, packageName);
        pro.put(EntityConstant.TABLES, tables);
        try {
            com.nk.generate.Generator.createGenerator(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String> tables = new ArrayList<String>();
        tables.add("t_user");
        tables.add("t_role");
        tables.add("t_menu");
        tables.add("t_user_role");
        tables.add("t_role_menu");
        GeneratorUtil.generator("mysql", "jdbc:mysql://bj-cdb-n8suw6od.sql.tencentcdb.com:5457/db_shop?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull",
                "root", "root@123", "t_", "c_", tables, "com.kun.shop.base.common",
                "IdEntity", "com.kun.shop.userMng");


        //GeneratorUtil.generator("oracle", "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "ztwd", "ztwd", "T_", "C_", tables, "com.nk.pa.frame.common", "IdEntity", "com.zt.pa.equip");
    }
}
